Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@material/checkbox
Advanced tools
@material/checkbox is a Material Design implementation of a checkbox component. It provides a customizable and accessible checkbox that can be used in web applications to allow users to make binary choices.
Basic Checkbox
This code demonstrates how to initialize a basic Material Design checkbox using the @material/checkbox package.
import {MDCCheckbox} from '@material/checkbox';
const checkbox = new MDCCheckbox(document.querySelector('.mdc-checkbox'));
Indeterminate State
This code shows how to set a checkbox to an indeterminate state, which is useful for indicating a mixed selection.
import {MDCCheckbox} from '@material/checkbox';
const checkbox = new MDCCheckbox(document.querySelector('.mdc-checkbox'));
checkbox.indeterminate = true;
Handling Change Events
This code demonstrates how to listen for change events on the checkbox and handle them accordingly.
import {MDCCheckbox} from '@material/checkbox';
const checkbox = new MDCCheckbox(document.querySelector('.mdc-checkbox'));
checkbox.listen('change', () => {
console.log('Checkbox state changed to: ', checkbox.checked);
});
react-checkbox-group is a React component for managing a group of checkboxes. It provides a simple API for handling multiple checkboxes and their states. Unlike @material/checkbox, which is a standalone Material Design component, react-checkbox-group is specifically designed for React applications and focuses on managing groups of checkboxes.
rc-checkbox is a React component for creating customizable checkboxes. It offers more flexibility in terms of styling and behavior compared to @material/checkbox. While @material/checkbox follows Material Design guidelines, rc-checkbox allows for more customization options.
react-toggle is a React component for creating toggle switches, which can be used as an alternative to checkboxes. It provides a different UI/UX compared to @material/checkbox but serves a similar purpose of allowing users to make binary choices. It is more suitable for applications that prefer toggle switches over traditional checkboxes.
The MDC Checkbox component is a spec-aligned checkbox component adhering to the Material Design checkbox requirements. It works without JavaScript with basic functionality for all states. If you use the JavaScript object for a checkbox, it will add more intricate animation effects when switching between states.
npm install --save @material/checkbox
<div class="mdc-checkbox">
<input type="checkbox"
class="mdc-checkbox__native-control"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark__path"
fill="none"
stroke="white"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
The checkbox component is driven by an underlying native checkbox element. This element is sized and positioned the same way as the checkbox component itself, allowing for proper behavior of assistive devices.
You can also add an mdc-checkbox--theme-dark
modifier class to the component to use the dark theme
checkbox styles.
Additionally, the checkbox can be used in conjunction with mdc-form-field to easily position checkboxes and their labels.
<div class="mdc-form-field">
<div class="mdc-checkbox">
<input type="checkbox"
id="my-checkbox"
class="mdc-checkbox__native-control"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark__path"
fill="none"
stroke="white"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
<label for="my-checkbox">My Checkbox Label</label>
</div>
<div class="mdc-checkbox mdc-checkbox--disabled">
<input type="checkbox"
id="basic-disabled-checkbox"
class="mdc-checkbox__native-control"
disabled />
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark__path"
fill="none"
stroke="white"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
<label for="basic-disabled-checkbox" id="basic-disabled-checkbox-label">This is my disabled checkbox</label>
Note that mdc-checkbox--disabled
is necessary on the root element to prevent hover states from activating.
MDC Checkbox ships with a Component / Foundation combo which progressively enhances the checkbox state transitions to achieve full parity with the Material Design motion for switching checkbox states.
import {MDCCheckbox, MDCCheckboxFoundation} from '@material/checkbox';
const mdcCheckbox = require('mdc-checkbox');
const MDCCheckbox = mdcCheckbox.MDCCheckbox;
const MDCCheckboxFoundation = mdcCheckbox.MDCCheckboxFoundation;
require(['path/to/mdc-checkbox'], mdcCheckbox => {
const MDCCheckbox = mdcCheckbox.MDCCheckbox;
const MDCCheckboxFoundation = mdcCheckbox.MDCCheckboxFoundation;
});
const MDCCheckbox = mdc.checkbox.MDCCheckbox;
const MDCCheckboxFoundation = mdc.checkbox.MDCCheckboxFoundation;
If you do not care about retaining the component instance for the checkbox, simply call attachTo()
and pass it a DOM element.
mdc.checkbox.MDCCheckbox.attachTo(document.querySelector('.mdc-checkbox'));
Checkboxes can easily be initialized using their default constructors as well, similar to attachTo
.
import {MDCCheckbox} from '@material/checkbox';
const checkbox = new MDCCheckbox(document.querySelector('.mdc-checkbox'));
The MDCCheckbox API provides accessor properties similar to those found on a native checkbox element.
Boolean. Returns whether or not the checkbox is checked. Setting this property will update the underlying checkbox element.
Boolean. Returns whether or not the checkbox is indeterminate. Setting this property will update the underlying checkbox element.
Boolean. Returns whether or not the checkbox is disabled. Setting this property will update the underlying checkbox element.
String. Returns the checkbox's value. Setting this property will update the underlying checkbox element.
MDC Checkbox ships with an MDCCheckboxFoundation
class that external frameworks and libraries can
use to integrate the component. As with all foundation classes, an adapter object must be provided.
The adapter for checkboxes must provide the following functions, with correct signatures:
Method Signature | Description |
---|---|
addClass(className: string) => void | Adds a class to the root element. |
removeClass(className: string) => void | Removes a class from the root element. |
registerAnimationEndHandler(handler: EventListener) => void | Registers an event handler to be called when an animationend event is triggered on the root element. Note that you must account for vendor prefixes in order for this to work correctly. |
deregisterAnimationEndHandler(handler: EventListener) => void | Deregisters an event handler from an animationend event listener. This will only be called with handlers that have previously been passed to registerAnimationEndHandler calls. |
registerChangeHandler(handler: EventListener) => void | Registers an event handler to be called when a change event is triggered on the native control (not the root element). |
deregisterChangeHandler(handler: EventListener) => void | Deregisters an event handler that was previously passed to registerChangeHandler . |
getNativeControl() => HTMLInputElement? | Returns the native checkbox control, if available. Note that if this control is not available, the methods that rely on it will exit gracefully. |
forceLayout() => void | Force-trigger a layout on the root element. This is needed to restart animations correctly. If you find that you do not need to do this, you can simply make it a no-op. |
isAttachedToDOM() => boolean | Returns true if the component is currently attached to the DOM, false otherwise. |
Returns whether or not the underlying input is checked. Returns false when no input is available.
Updates the checked
property on the underlying input. Does nothing when the underlying input is
not present.
Returns whether or not the underlying input is indeterminate. Returns false when no input is available.
Updates the indeterminate
property on the underlying input. Does nothing when the underlying input
is not present.
Returns whether or not the underlying input is disabled. Returns false when no input is available.
Updates the disabled
property on the underlying input. Does nothing when the underlying input is
not present.
Returns the value of adapter.getNativeControl().value
. Returns null
if getNativeControl()
does not return an object.
Sets the value of adapter.getNativeControl().value
. Does nothing if getNativeControl()
does
not return an object.
MDC Checkboxes use the theme's primary color by default for checked and indeterminate states, and are completely dark theme aware.
In browsers that fully support CSS variables, MDC Checkbox references CSS variables wherever theme properties are used.
However, due to Edge's buggy CSS variable support, the background-color
for .mdc-checkbox__background::before
will not honor CSS variables in Edge.
This means you will need to override this style manually for Edge if you alter the CSS variable for the primary color.
FAQs
The Material Components for the web checkbox component
The npm package @material/checkbox receives a total of 323,675 weekly downloads. As such, @material/checkbox popularity was classified as popular.
We found that @material/checkbox demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 15 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.